package patrol.mobile.com.mobilepatrol.fragment;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import patrol.mobile.com.mobilepatrol.DBHelper;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.route_query.RouteType;
import patrol.mobile.com.mobilepatrol.activity.route_query.TotalRouteActivity;
import patrol.mobile.com.mobilepatrol.activity.route_query.TypeAdapter;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.StringUtils;

/**
 * Created by apple on 18/1/24.
 */

public class RouteListFragment extends BaseFragment {
    private DBHelper db;
    @BindView(R.id.el)
    ExpandableListView el;

    @BindView(R.id.tv_org)
    TextView tv_org;

    @BindView(R.id.tv_mail)
    TextView tv_mail;

    @BindView(R.id.title)
    View title;
    private List<RouteType> mList;
    private TotalRouteActivity mContext;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext  = (TotalRouteActivity) context;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_route;
    }

    @Override
    protected void initData() {
        title.setVisibility(View.GONE);
        db = new DBHelper(getActivity());
        tv_org.setText(UserManager.getUser().bmname);
        tv_mail.setText("共"+String.format("%.2f", db.getRouteManageMiles())+"km");
        List<String> types = db.getRouteManageTypes();
        if (types.size()==0){
            showToast("请初始化路线查询单位信息");
            return;
        }

        mList = db.getTypes(StringUtils.sortList(db.getRouteManageTypes()));
        TypeAdapter mAdapter = new TypeAdapter(getActivity(),mList);
        el.setAdapter(mAdapter);
        //遍历所有group,将所有项设置成默认展开
        for (int i=0; i<mList.size(); i++)
        {
            el.expandGroup(i);
        }
    }

    @Override
    protected void initListener() {
        switch (mContext.from){
            case 1:
                break;
            case 2:
                el.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                    @Override
                    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                        Intent intent = new Intent();
                        intent.putExtra("route",mList.get(groupPosition).routeManages.get(childPosition));
                        getActivity().setResult(1,intent);
                        CommonUtil.exitActivityAndBackAnim(getActivity(),true);
                        return false;
                    }
                });
                break;
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        db.close();
    }
}
